////////////////////////////////////////////////////////////////////////////////
/// @file    play_sinewave.c
/// @author  AE TEAM
/// @brief   THIS FILE PROVIDES ALL THE play_sinewave FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////
// Define to prevent recursive inclusion
#define _PLAY_SINEWAVE_C_

// Files includes

#include "HAL_conf.h"
#include "HAL_device.h"
#include <stdio.h>
#include <string.h>
#include "bsp_WM8978.h"

#include "play_sinewave.h"
#include "sdio.h"
#include "ff.h"
#include "fatfs.h"
#include "sdio_sdcard.h"

#include "uart.h"
#include "delay.h"

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Example_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup PLAY_SINEWAVE
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup PLAY_SINEWAVE_Exported_Constants
/// @{

#define FMT_COUNT   6
uint32_t g_FmtList[FMT_COUNT][3] = {
    {I2S_Standard_Phillips, I2S_DataFormat_16b, I2S_AudioFreq_8k},
    {I2S_Standard_Phillips, I2S_DataFormat_16b, I2S_AudioFreq_16k},
    {I2S_Standard_Phillips, I2S_DataFormat_16b, I2S_AudioFreq_22k},
    {I2S_Standard_Phillips, I2S_DataFormat_16b, I2S_AudioFreq_44k},
    {I2S_Standard_Phillips, I2S_DataFormat_16b, I2S_AudioFreq_96k},
    {I2S_Standard_Phillips, I2S_DataFormat_16b, I2S_AudioFreq_192k},
};

#define DataFormat_16   1
#define DataFormat_24   0
#define DataFormat_32   0

#define Standard_Phillips   1
#define Standard_MSB   0
#define Standard_LSB   0
#define Standard_PCM   0



s16 I2sSendu16[32 * 2 * 2] = {
    0, 0,
    3204, 3204,
    6285, 6285,
    9124, 9124,
    11612, 11612,
    13655, 13655,
    15172, 15172,
    16107, 16107,
    16423, 16423,
    16107, 16107,
    15172, 15172,
    13655, 13655,
    11613, 11613,
    9124, 9124,
    6285, 6285,
    3204, 3204,
    0, 0,
    -3204, -3204,
    -6285, -6285,
    -9124, -9124,
    -11612, -11612,
    -13655, -13655,
    -15172, -15172,
    -16107, -16107,
    -16423, -16423,
    -16107, -16107,
    -15172, -15172,
    -13655, -13655,
    -11613, -11613,
    -9124, -9124,
    -6285, -6285,
    -3204, -3204,
    0, 0,
    3204, 3204,
    6285, 6285,
    9124, 9124,
    11612, 11612,
    13655, 13655,
    15172, 15172,
    16107, 16107,
    16423, 16423,
    16107, 16107,
    15172, 15172,
    13655, 13655,
    11613, 11613,
    9124, 9124,
    6285, 6285,
    3204, 3204,
    0, 0,
    -3204, -3204,
    -6285, -6285,
    -9124, -9124,
    -11612, -11612,
    -13655, -13655,
    -15172, -15172,
    -16107, -16107,
    -16423, -16423,
    -16107, -16107,
    -15172, -15172,
    -13655, -13655,
    -11613, -11613,
    -9124, -9124,
    -6285, -6285,
    -3204, -3204
};


void FillDataMode0(void)
{

    I2S_TX_DMAPolling_Circular_Auto_Reload_Enable_Init((u16*)&I2sSendu16[0], 128);
    DMA_Cmd(DMA1_Channel3, ENABLE);

    while(1) {
        ;
    }
}

s16 I2sSendBuffer[32 * 2 * 2];
s16 I2sSendBuffer2[32 * 2 * 2];

void FillDataMode2(void)
{
    memcpy((u8*)I2sSendBuffer, (u8*)I2sSendu16, 128);
    I2S_TX_DMAPolling_Circular_Auto_Reload_Enable_Init((u16*)&I2sSendBuffer[0], 128);
    DMA_Cmd(DMA1_Channel3, ENABLE);

    while(1) {
        if(DMA_GetITStatus(DMA1_IT_HT3) == SET) {
            //        aotu_flag1=1;
            DMA_ClearITPendingBit(DMA1_IT_HT3);
            memcpy((u8*)I2sSendBuffer, (u8*)I2sSendu16, 64);
//            if(T1 == 0) {
//                exDMA_SetPeripheralAddress(DMA1_Channel3, (u32)buffer2);
//                buff1_flag = 1;
//                //exDMA_SetTransmitLen(DMA1_Channel3,256);
//            }
//            else {
//                //memset(&buffer2[0],0,10240);
//                exDMA_SetPeripheralAddress(DMA1_Channel3, (u32)buffer3);
//                buff2_flag = 1;
//                //exDMA_SetTransmitLen(DMA1_Channel3,256);
//            }
        }
        else if(DMA_GetITStatus(DMA1_IT_TC3) == SET) {
            //        aotu_flag1=1;
            DMA_ClearITPendingBit(DMA1_IT_TC3);
            memcpy((u8*)((u32)I2sSendBuffer[0] + 64), (u8*)((u32)I2sSendu16[0] + 64), 64);
//            if(T1 == 0) {
//                exDMA_SetPeripheralAddress(DMA1_Channel3, (u32)buffer2);
//                buff1_flag = 1;
//                //exDMA_SetTransmitLen(DMA1_Channel3,256);
//            }
//            else {
//                //memset(&buffer2[0],0,10240);
//                exDMA_SetPeripheralAddress(DMA1_Channel3, (u32)buffer3);
//                buff2_flag = 1;
//                //exDMA_SetTransmitLen(DMA1_Channel3,256);
//            }
        }
    }
}


void FillDataMode1b(void)
{
    bool switchflag = 0;
    memcpy((u8*)I2sSendBuffer, (u8*)I2sSendu16, 128);
    memcpy((u8*)I2sSendBuffer2, (u8*)I2sSendu16, 128);

    I2S_TX_DMAPolling_Circular_Auto_Reload_Enable_Init((u16*)&I2sSendBuffer[0], 128);
    switchflag = 1;
    DMA_Cmd(DMA1_Channel3, ENABLE);

    while(1) {
        if(DMA_GetITStatus(DMA1_IT_TC3) == SET) {
            DMA_ClearITPendingBit(DMA1_IT_TC3 | DMA1_IT_GL3);
            if(switchflag == 1) {
                //exDMA_SetMemoryAddress(DMA1_Channel3, (u32)I2sSendBuffer2);
                DMA1_Channel3->CMAR = (u32)&I2sSendBuffer2[0];
                switchflag = 0;
            }
            else {
                //exDMA_SetMemoryAddress(DMA1_Channel3, (u32)I2sSendBuffer);
                DMA1_Channel3->CMAR = (u32)&I2sSendBuffer[0];
                switchflag = 1;
            }
        }

    }
}

void FillDataMode1OK(void)
{
    memcpy((u8*)I2sSendBuffer, (u8*)I2sSendu16, 128);
    I2S_TX_DMAPolling_Circular_Auto_Reload_Enable_Init((u16*)&I2sSendBuffer[0], 64);
    DMA_Cmd(DMA1_Channel3, ENABLE);

    while(1) {
        if(DMA_GetITStatus(DMA1_IT_HT3) == SET) {
            //        aotu_flag1=1;
            DMA_ClearITPendingBit(DMA1_IT_HT3 | DMA1_IT_GL3);
            memcpy((u8*)(&I2sSendBuffer[0]), (u8*) & (I2sSendu16[0]), 64);
//            if(T1 == 0) {
//                exDMA_SetPeripheralAddress(DMA1_Channel3, (u32)buffer2);
//                buff1_flag = 1;
//                //exDMA_SetTransmitLen(DMA1_Channel3,256);
//            }
//            else {
//                //memset(&buffer2[0],0,10240);
//                exDMA_SetPeripheralAddress(DMA1_Channel3, (u32)buffer3);
//                buff2_flag = 1;
//                //exDMA_SetTransmitLen(DMA1_Channel3,256);
//            }
        }
        else if(DMA_GetITStatus(DMA1_IT_TC3) == SET) {
            //        aotu_flag1=1;
            DMA_ClearITPendingBit(DMA1_IT_TC3 | DMA1_IT_GL3);
            memcpy((u8*)((u32)(&I2sSendBuffer[0]) + 64), (u8*)((u32)(&I2sSendu16[0]) + 64), 64);
//            if(T1 == 0) {
//                exDMA_SetPeripheralAddress(DMA1_Channel3, (u32)buffer2);
//                buff1_flag = 1;
//                //exDMA_SetTransmitLen(DMA1_Channel3,256);
//            }
//            else {
//                //memset(&buffer2[0],0,10240);
//                exDMA_SetPeripheralAddress(DMA1_Channel3, (u32)buffer3);
//                buff2_flag = 1;
//                //exDMA_SetTransmitLen(DMA1_Channel3,256);
//            }
        }
    }
}
void initI2S_CodecMode1(void)
{
    delay_init();
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOE, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBENR_SDIO, ENABLE);
    uart_initwBaudRate(115200);

    wm8978_Init();

    I2S_CODEC_Init();

    StartPlay();

}

void PlaySineDmaPollingDemo(void)
{
    initI2S_CodecMode1();
    FillDataMode1OK();
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Handle DMA interrupt
/// @param  None
/// @retval None
////////////////////////////////////////////////////////////////////////////////
void DMA1_Channel3_IRQHandler(void)
{
    if(DMA_GetITStatus(DMA1_IT_TC3) == SET) {
        DMA_ClearITPendingBit(DMA1_IT_TC3);

    }
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  Configure WM8978 and I2S to start playing.
/// @param  None
/// @retval None
////////////////////////////////////////////////////////////////////////////////
void StartPlay(void)
{
    int i;

    i = 100000;
    while(i--);
    wm8978_Init();
    I2S_Stop();




//  wm8978_Dac2Ear();
    wm8978_Dac2Spk();


    wm8978_ChangeVolume(20, 20);


    wm8978_CfgAudioIF(I2S_Standard_Phillips, 16, SPI_Mode_Master);


    I2S_StartPlay((SPI_I2S_STANDARD_TypeDef)g_FmtList[0][0], (SPI_I2S_DATAFORMAT_TypeDef)g_FmtList[0][1], I2S_AudioFreq_48k);
}


/// @}

/// @}

/// @}

